package com.yt.system.dao;

import com.yt.system.domain.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import javax.crypto.KeyGenerator;
import java.util.List;

/**
 * @author 高铭
 * @date 2019/9/18 - 16:00
 */
@Mapper
@Repository
public interface UserMapper {

    int deleteByPrimaryKey(Integer uid);

    int insert(User record);

    int insertSelective(User record);

    /**
     * 根据用户ID查询用户信息
     * @param uid
     * @return
     */
    User selectByPrimaryKey(Integer uid);

    /**
     * 根据用户ID有选择的更改用户信息
     * @param record
     * @return
     */
    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    /**
     * 查询所有用户的信息
     * @return
     */
    @Select("select uid,uname from user")
    List<User> adminFindAllUser();

    /**
     * 随机分组时候，将上周的分组信息表状态设置为1(代表过去的)
     */
    @Update("update group_message set status = 1 where status = 0")
    void updateGroupMessageStatus();

    /**
     * 携带 组别 i 以及 uid 去操作数据，保存随机分组后组员和组别的关系,
     *  并默认数据库group_message表中的ststus状态是0，代表是今周的分组情况
     * @param uid
     * @param gid
     */
    @Insert("insert into group_message values (null,#{uid},#{gid},status)")
    void adminRandomToAllGroup(@Param("uid") Integer uid, @Param("gid") Integer gid);

    @Select("select * from user where uid not in (select uid from clock where entertime like CONCAT(curdate(),'%') or leavetime like CONCAT(curdate(),'%'))")
    List<User> findNoClockUserByTime();

    /**
     * 查询所有用户信息
     * @return
     */
    List<User> getAllUser();

    /**
     * 根据uemail查询用户（用户与uemail为一一对应关系）
     */
    @Select("select * from user where uemail = #{uemail}")
    User selectByEmail(String uemail);

    /**
     * 查找除自己之外的当前小组的其他人员信息
     * @param uid
     * @param gid
     * @return
     */
    @Select("select uid,uname from user where uid in (select uid from group_message where uid != #{uid} " +
            "and gid = #{gid} and status = 0)")
    List<User> getGroupOtherUser(@Param("uid") Integer uid,@Param("gid") Integer gid);

    /**
     * 将组别表的状态更改为1，代表组别表的信息过去了
     */
    @Update("update grouptable set gstatus = 1 where gstatus = 0")
    void updateGroupTableStatus();

    /**
     * 组长为组别添加口号和组别名称
     * @param groupTable
     */
    @Insert("insert into grouptable values (null,#{gid},null,#{gname},#{gslogan},null,#{gstatus})")
    void leaderInsertGroupMsg(GroupTable groupTable);

    /**
     * 组长更新当周组别的信息
     * @param groupTable
     */
    @Update("update grouptable set gname = #{gname},gslogan = #{gslogan} where gid = #{gid}")
    void leaderUpdateGroupMsg(GroupTable groupTable);
    /**
     * 查询出这周组别的集合
     * @return
     */
    @Select("select distinct gid from group_message where status = 0")
    List<Integer> getAllTheGid();

    /**
     * 根据gid查询到相对应的组别口号，组名称等等信息
     * @param gid
     * @return
     */
    @Select("select * from grouptable where gid = #{gid}")
    List<GroupTable> getGroupTableMsg(Integer gid);

    /**
     * 根据组别的gid去查询这组用户的人员信息
     * @param gid
     * @return
     */
    @Select("select usertable.uid, usertable.uname,usertable.usex,subject.sname,usertable.uphone,user_role.roid from " +
            "(select uid,uname,usex,sid,uphone from user where uid in (select uid from group_message where gid = #{gid} " +
            "and status = 0)) as usertable left join user_role on user_role.aid = usertable.uid inner join subject on subject.sid = usertable.sid")
    List<UserAndRoleMsg> UserAndRoleMsg(Integer gid);

    /**
     * 选好并确认组长
     * @param uid
     * @param gid
     */
    @Insert("insert into user_role values (null,#{uid},2,#{gid})")
    void sureGroupLeader(Integer uid, Integer gid);

    @Delete("delete from user_role where gid = #{gid}")
    void deleteGroupLeader(Integer gid);

    @Select("select * from grouptable where gid = #{gid} and gstatus = 0")
    GroupTable getGroupTable(Integer gid);

    @Select("select * from user where uid in (select uid from group_message where gid = #{gid} and status = 0)")
    List<User> getGroupTableList(Integer gid);

    /**
     * 对某个组进行组别重置，将这个组别的状态修改成为历史
     * @param gid
     */
    @Update("update grouptable set gstatus = 1 where gstatus = 0 and gid = #{gid}")
    void resetTheGroup(Integer gid);

    /**
     *  修改这组人员的  组别--人员 状态,代表将这组人员的信息状态改变为过去
     * @param gid
     */
    @Update("update group_message set status = 1 where status = 0 and gid = ${gid}")
    void resetTheGroupPeopleMsg(Integer gid);

    /**
     *  对某个人进行组别重置（将这个人从组别中移除出去），
     *  将移除的这个人添加到一张临时表，方便下次对这些个人移除的人员进行分组
     * @param uid
     * @param gid
     * @return
     */
    @Update("update group_message set status = 1 where uid = #{uid} and gid = #{gid} and status = 0")
    int resetTheGroupOnePeople(@Param("uid") Integer uid, @Param("gid") Integer gid);

    /**
     * 管理员对全部人员随机分组的时候
     *      将用户-角色表的数据全部删除（保留管理员和超级管理员），避免选出组长出现数据重复（与上周冲突）
     */
    @Delete("delete from user_role where roid = 2")
    void deleteUserRole();

    /**
     * 将user_role用户-角色表中的对应数据删除，避免了下周组长出现数据冲突
     * @param gid
     */
    @Delete("delete from user_role where gid = #{gid}")
    void deleteUserRoleByGid(Integer gid);

    /**
     * 判断用户是否是组长
     * @param uid
     * @param gid
     * @return
     */
    @Select("select * from user_role where aid = #{uid} and gid = #{gid}")
    UserRole userIsLeader(@Param("uid") Integer uid,@Param("gid") Integer gid);

    /**
     * 判断这个组是否有组长
     * @param gid
     * @return
     */
    @Select("select * from user_role where gid = #{gid}")
    UserRole groupIsHaveLeader(Integer gid);

    /**
     *  根据uid查找用户
     * @param uid
     * @return
     */
    @Select("select * from user where uid = #{uid}")
    User getUserByUid(Integer uid);

    /**
     * 将重置的人员信息保存在临时表中
     * @param user
     */
    @Insert("insert into usertemplate values (null,#{uid},#{uname},#{usex},#{uphone},#{sid})")
    void saveUserToUserTemplate(User user);

    /**
     *通过组id去得到当周有效组的所以组员
     * @param gid
     * @return
     */
    @Select("select * from user where uid in (select uid from group_message where gid = #{gid} and status = 0)")
    List<User> getUserByGid(Integer gid);

    /**
     * 查找重置分组之后处于没安排组别状态的所有人员
     * @return
     */
    @Select("select * from usertemplate")
    List<User> findAllNoInGroupPeople();

    /**
     将某个重置分组之后处于没安排组别状态的人员拉进具体的组别里,
     顺便将这个人员——组别的状态设置为 0 ，代表是这个周有效的人员
     * @param uid
     * @param gid
     */
    @Insert("insert into group_message values (null,#{uid},#{gid},0)")
    void pullPeopleToGroup(Integer uid, Integer gid);

    /**
     * 将游离状态下的某个用户从usertemplate临时表中删除
     * @param uid
     */
    @Delete("delete from usertemplate where uid = #{uid}")
    void delPeopleFromUserTemplateByUid(Integer uid);

    /**
     * 管理员对所有人重置组别时候，删除用户临时表
     */
    @Delete("delete from usertemplate")
    void delUserTemplate();

    @Update("update user set img = #{img} where uid = #{uid}")
    int saveImg(Integer uid,String img);


    /**
     * //清除所有的组别账号排名表
     */
    @Delete("delete from group_account")
    void delGroupAccount();

    /**
     * 分组后将新的组信息插进group_account表中
     * @param groupAccount
     */
    @Insert("insert into group_account values (null,#{gid},#{gname},#{gscore})")
    void insertGroupAccount(GroupAccount groupAccount);

    /**
     * //修改这个组别的分数为0
     * @param gid
     */
    @Update("update group_account set gscore = 0 where gid = #{gid}")
    void updateGroupAccount(Integer gid);


}